package demo6;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-02-10
 * Time: 0:57
 */
////使用comparable接口，比较学生类
//class Student implements Comparable<Student>{
//    public String name;
//    public int age;
//    public double score;
//
//    public Student(String name, int age, double score) {
//        this.name = name;
//        this.age = age;
//        this.score = score;
//    }
//
//    @Override
//    public String toString() {
//        return "Student{" +
//                "name='" + name + '\'' +
//                ", age=" + age +
//                ", score=" + score +
//                '}';
//    }
//
//    //额外指定数组比较的内容
//    @Override
//    public int compareTo(Student o) {
//        //按年龄比较
//        //谁调用compareTo方法，谁就是this
//
//       /* if(this.age>o.age){
//            return 1;
//        }else if(this.age<o.age){
//            return -1;
//        }else {
//            return 0;
//        }*/
//
//        //代码简化
//        return this.age - o.age;     //从小到大排序
//       // return o.age - this.age;  //从大到小排序
//
//        //根据分数比较
//        //成绩是double类型的，所以要进行强制类型转换
//      // return (int)(this.score - o.score);
//
//        //根据姓名比较
//        //要是用 compareTo() 方法进行比较
//        //return this.name.compareTo(o.name);
//    }
//}

class Student {
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}
//按年龄进行比较
class AgeComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}
//按分数进行比较
class ScoreComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return (int)(o1.score - o2.score);
    }
}
//按姓名进行排序
class NameComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",20,89.6);
        students[1] = new Student("lisi",22,77.6);
        students[2] = new Student("wangwu",19,66.6);
        System.out.println("比较前：");
        System.out.println(Arrays.toString(students));

        AgeComparator ageComparator = new AgeComparator();
        ScoreComparator scoreComparator = new ScoreComparator();
        NameComparator nameComparator = new NameComparator();
        System.out.println("比较后：");
        Arrays.sort(students,nameComparator);
        System.out.println(Arrays.toString(students));
    }
}
